package com.zj.aspects;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.zj.service.RedPacketService;
import com.zj.web.bean.MyUserDTO;
import com.zj.web.bean.UserRedPacketDTO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
@Log
public class JmsRedPacketMessageConsumer {
    /**
     * 监听队列名为userLogin的消息，然后处理
     * @param message
     */
    @Autowired
    private RedPacketService redPacketService;
    @JmsListener(destination = "${spring.activemq.redpacketQueue}")
    public void receiveMessage(String message) {
        log.info("收到消息: " + message);
        Gson gson = new Gson();
        UserRedPacketDTO userRedPacketDTO = gson.fromJson(message, UserRedPacketDTO.class);  //将消息转换为用户对象,跟切面保持一种
        log.info("抢红包成功，将要插入数据库的数据是： " + userRedPacketDTO.toString());
        //将用户抢红包的信息添加到数据库
        redPacketService.addUserRedPacket(userRedPacketDTO);
    }
}
